// Copyright (c) 2009 http://grommet.codeplex.com
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.

using System;
using Microsoft.SPOT;
using System.Collections;

namespace System.Net
{
    public class WebHeaderCollection : ICollection
    {
        private class NameValuePair
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }

        public class WebHeaderEnumerator : IEnumerator
        {
            private WebHeaderCollection headerCollection;
            private int index = -1;

            public WebHeaderEnumerator(WebHeaderCollection collection)
            {
                headerCollection = collection;
            }

            #region IEnumerator Members

            public object Current
            {
                get
                {
                    return headerCollection[index];
                }
            }

            public bool MoveNext()
            {
                index++;
                if (index < headerCollection.Count)
                {
                    return true;
                }
                else
                {
                    index = -1;
                    return false;
                }

            }

            public void Reset()
            {
                index = -1;
            }

            #endregion
        }

        private ArrayList headers = new ArrayList();
        private object syncRoot = new object();

        public string this[int index]
        {
            get { return Get(index); }
        }

        public string this[string name]
        {
            get { return Get(name); }
            set { Set(name, value); }
        }

        public string[] AllKeys
        {
            get { return (string[])GetKeys().ToArray(typeof(string)); }
        }

        public ArrayList Keys
        {
            get { return GetKeys(); }
        }

        public void Add(string name, string value)
        {
            AddWithoutValidate(name, value);
        }

        protected void AddWithoutValidate(string name, string value)
        {
            headers.Add(new NameValuePair() { Name = name, Value = value });
        }

        public void Clear()
        {
            headers.Clear();
        }

        public string Get(int index)
        {
            return ((NameValuePair)headers[index]).Value;
        }

        public string Get(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                NameValuePair nvp = (NameValuePair)headers[i];
                if (nvp.Name == name)
                {
                    return nvp.Value;
                }
            }
            return null;
        }

        public string GetKey(int index)
        {
            return ((NameValuePair)headers[index]).Name;
        }

        public void Remove(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                NameValuePair nvp = (NameValuePair)headers[i];
                if (nvp.Name == name)
                {
                    headers.RemoveAt(i);
                    break;
                }
            }
        }

        public void Set(string name, string value)
        {
            for (int i = 0; i < Count; i++)
            {
                NameValuePair nvp = (NameValuePair)headers[i];
                if (nvp.Name == name)
                {
                    nvp.Value = value;
                    return;
                }
            }
            Add(name, value);
        }

        private ArrayList GetKeys()
        {
            ArrayList list = new ArrayList();
            foreach (NameValuePair nvp in headers)
            {
                list.Add(nvp.Name);
            }
            return list;
        }

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            headers.CopyTo(array, index);
        }

        public int Count
        {
            get { return headers.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return syncRoot; }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return new WebHeaderEnumerator(this);
        }

        #endregion
    }

}
